package zuk.clipvuiviet.clipadapter;

import java.util.List;

import zuk.clipvuiviet.R;
import zuk.clipvuiviet.loadImageView.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class ClipAdapter extends ArrayAdapter<Clip>{
	ImageLoader imageLoader;
	private List<Clip> clips;
	private boolean mHasMoreItems;
	private String tab;
	int i = 2;

	public List<Clip> getClips() {
		return clips;
	}
	
	public ClipAdapter(Context context, int textViewResourceId,String tap,
			List<Clip> clips) {
		super(context, textViewResourceId, clips);
		this.clips = clips;
		this.tab = tap;
		imageLoader = new ImageLoader(context);
	}
	
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		if (position == getCount() - 1 && mHasMoreItems) {
			LoadClipEnlessAdapterTask t = new LoadClipEnlessAdapterTask(i,
					getContext(), tab, this);
			t.execute();
			i++;

		}
		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = infalInflater.inflate(R.layout.clip, null);
		}
		ImageHolder imgHolder = new ImageHolder();
		TextView tViewTitle = (TextView)v.findViewById(R.id.text_title);
		
		Clip clip = clips.get(position);
		tViewTitle.setText(clip.getTitle());
		
		imgHolder.img_thumb = (ImageView)v.findViewById(R.id.img_thumb);
		imgHolder.img_thumb.setTag(position);
		imgHolder.prgBar = (ProgressBar)v.findViewById(R.id.prgBar);
		imgHolder.img_url = clip.imgurl;
		imgHolder.position = position;
		imageLoader.DisplayImage(imgHolder.img_url, imgHolder.img_thumb,
				imgHolder.prgBar);
//		new LoadImageTask(imgHolder).execute();
		return v;
	}
	

}
